#include "compiler.hpp"
#include "runner.hpp"
#include "compile_run.hpp"
#include "../comm/httplib.h"
#include <jsoncpp/json/json.h>
using namespace ns_runner;
using namespace ns_compiler;
using namespace ns_compile_run;
using namespace httplib;
int main(int argc,char* args[])
{
    if(argc!=2)
    {
        std::cout<<"Usage: "<<"./compile_server port"<<std::endl;
        return -1;
    }
    Server svr;
    svr.Post("/compile_and_run", [](const Request &req, Response &resp)
             {
                 std::string in_json = req.body;
                 std::string out_json;
                 if (!in_json.empty())
                 {
                     CompileAndRun::Start(in_json, &out_json);
                     resp.set_content(out_json,"application/json;charset=utf-8");
                 }
             });
    int port=atoi(args[1]);
    svr.listen("0.0.0.0",port);
    return 0;
}
// int main()
// {
//     // 编写一段code
//     // in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}
//     // out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}
//     Json::Value in_value;
//     in_value["code"]=R"(#include<iostream>
// int main()
// {
//     std::cout<<"hello world"<<std::endl;
//     return 0;
// }
//     )";
//     in_value["input"]="";
//     in_value["cpu_limit"]=1;
//     in_value["mem_limit"]=10240;
//     Json::StyledWriter Write;
//     std::string in_json=Write.write(in_value);
//     std::string out_json;
//     CompileAndRun::Start(in_json, &out_json);
//     return 0;
// }